package com.uwo.house.controller;

import com.uwo.house.beans.request.DecorationPlanReq;
import com.uwo.house.beans.response.ConstructionScheduleResp;
import com.uwo.house.beans.response.DecorationPlanResp;
import com.uwo.house.beans.response.MaterialListResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.IDecorationSchemeService;
import com.uwo.house.util.UtilReturn;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 装修方案controller
 * Created by YZYG on 2017/7/17.
 */
@Controller
@RequestMapping("/decoration")
public class DecorationSchemeController extends BaseController {
    @Autowired
    public IDecorationSchemeService decorationSchemeService;

    /**
     * 用户中心-查询装修方案
     *
     * @param planSts
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getDecorationList(@RequestParam("planSts") String planSts,
                                 @RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        String userId = jwtToken.getUser().getId();
        List<DecorationPlanResp> list = this.decorationSchemeService.getDecorationList(userId, planSts);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 用户中心-装修方案-点击漫游图 获取订单详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    @ResponseBody
    public Map getDecorationDetails(@RequestParam("id") String id) {
        DecorationPlanResp decorationPlanResp = this.decorationSchemeService.getDecorationDetails(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, decorationPlanResp);
    }

    /**
     * 用户中心-装修方案-点击漫游图 获取订单详情-获取材料清单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/material/details", method = RequestMethod.GET)
    @ResponseBody
    public Map getDecorationMaterialDetails(@RequestParam("id") int id) {
        List<MaterialListResp> list= this.decorationSchemeService.getDecorationMaterialDetails(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * csv解析
     *
     * @param path
     * @return
     */
    @RequestMapping(value = "/csv", method = RequestMethod.GET)
    public Map getCsvDetails(@RequestParam("path") String path) {
        return null;
    }

    /**
     * 用户确认装修方案
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/confirm", method = RequestMethod.PUT)
    @ResponseBody
    public Map confirmPlan(@RequestBody DecorationPlanReq req,
                           @RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        String userId = jwtToken.getUser().getId();
        int result = this.decorationSchemeService.confirmPlan(req.getId());
        this.decorationSchemeService.abandonPlan(req.getOrderId(), req.getId());
        this.decorationSchemeService.addMaterialStatus(req.getOrderId(), req.getId());
        if (result == 1) {
            this.addRecord("我的新房设计完成并完成在线签约", userId,"","");
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "确认成功");
        }
        return UtilReturn.resErrorMap("确认失败",UtilReturn.ERROR_CODE);
    }

    /**
     * 装修方案中查看材料清单
     *
     * @param orderId
     * @param planId
     * @return
     */
    @RequestMapping(value = "/material", method = RequestMethod.GET)
    @ResponseBody
    public Map getMaterialList(@RequestParam("orderId") String orderId,
                               @RequestParam("planId") int planId) {
        String MaterialAttachment = this.decorationSchemeService.getMaterialList(orderId, planId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, MaterialAttachment);
    }

    /**
     * 进行中方案-获取施工进度
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/schedule", method = RequestMethod.GET)
    @ResponseBody
    public Map getConstructionSchedule(@RequestParam("orderId") String orderId) {
        List<ConstructionScheduleResp> list = this.decorationSchemeService.geConstructionSchedule(orderId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 进行中方案-验收
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseBody
    public Map checkProject(@RequestParam("id") String id) {
        int result = this.decorationSchemeService.checkProject(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "验收成功");
        }
        return UtilReturn.resErrorMap("验收失败",UtilReturn.ERROR_CODE);
    }

}
